package com.controller;

import java.io.IOException;
import java.util.*;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;

import com.bean.*;
import com.dao.*;

/**
 * 关于博客的所有操作,与blog数据表紧密关联
 * 
 * @author hhh
 *
 */
@WebServlet("/BlogEdit")
public class BlogEdit extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3005015380659346113L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		HttpSession session = req.getSession();
		User user = (User) session.getAttribute("pass");
		String action = req.getParameter("action");
		if ("show".equals(action)) {// 展示博客标题
			List<Blog> list =new BlogSQL().findTitle(user.getId());
			session.setAttribute("titles", list);
			resp.sendRedirect("home.jsp");
		} else if ("readBlog".equals(action)) {// 阅读博客内容
			int id = Integer.parseInt(req.getParameter("id"));
			Blog blog = new BlogSQL().find(id);		//找到博客
			req.setAttribute("blog", blog);
			UserSQL us=new UserSQL();
			user = us.find(blog.getUid());
			ArrayList<Comment> comment=new CommentSQL().findByBlog(blog.getId());//取出博客所有一级评论
			//List<Comment> ll=null;
			for(Comment li:comment){
				li.setAllList();
				for(Comment lli:li.getList()){
					if(lli.getCcid()!=li.getId()){
						lli.setTitle(lli.getTitle()+"回复:"+us.find(lli.getUid()).toString());
					}
				}
				}
			req.setAttribute("comment",comment);
			req.setAttribute("author", user.getName());
			req.getRequestDispatcher("read.jsp").forward(req, resp);
		} else if ("keys".equals(action)) {		//关键字搜索博客
			String key = req.getParameter("key");
			List<Blog> list = new BlogSQL().findByKey(key);
			session.setAttribute("titles", list);
			resp.sendRedirect("home.jsp");
		}else if("change".equals(action)){		//修改博客
			int id = Integer.parseInt(req.getParameter("id"));
			Blog blog = new BlogSQL().find(id);
			req.setAttribute("blog", blog);
			req.getRequestDispatcher("writeblog.jsp?action=update").forward(req, resp);
		}else if("delete".equals(action)){
			int id=Integer.parseInt(req.getParameter("id"));
			new BlogSQL().delete(id);
		}
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		HttpSession session = req.getSession();
		String action = req.getParameter("action");
		String content = req.getParameter("note");
		User user = (User) session.getAttribute("pass");
		int uid = user.getId();
		String str = "";
		if ("blog".equals(action)) {// 判断行为,是否上传博客
			if (content != "") { // 内容不能为空
				String title = req.getParameter("title");
				if ("".equals(title))//默认设置标题
					title =(content+"          ").substring(0, 10);
				Blog blog = new Blog(title, content);
				if (new BlogSQL().addBlog(uid, blog))// 存入数据库并判断是否成功
					str = "succeed!";
				else
					str = "fail!";
			}
		}else if("update".equals(action)){
			if (content != "") { // 内容不能为空
				String title = req.getParameter("title");
				if ("".equals(title))
					title =(content+"          ").substring(0, 10);
				int id = Integer.parseInt(req.getParameter("id"));
				Blog blog = new Blog(id,title, content);
				new BlogSQL().update(blog);// 修改数据库并判断是否成功
					str = "succeed!";
			}
		}
		resp.sendRedirect("writeblog.jsp?msg="+str);
	}

	@Override
	protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		super.doDelete(req, resp);
	}

}
